package com.lemon.components.ant.project.makers;

import java.util.ArrayList;
import java.util.List;

import com.lemon.components.ant.project.beans.ColumnInfo;
import com.lemon.components.ant.project.beans.MethodInfo;
import com.lemon.components.ant.project.beans.ParamInfo;
import com.lemon.components.ant.util.ConfigUtil;
import com.lemon.components.ant.util.TemplateUtil;
import com.lemon.components.docs.mysql.MysqlLocalTool;
import com.lemon.ui.codemaker.component.RecentlyFile;
import com.system.core.security.NameUtil;

public class MethodMaker {
	

	public static void make() {
		MethodInfo methodInfo = ConfigUtil.getInstance(RecentlyFile.configFilePath).getMethodInfo();
		for(String param:methodInfo.getParams().keySet()) {
			List<ColumnInfo> tmp = new ArrayList<ColumnInfo>();
			for(String tableName:methodInfo.getParams().get(param)) {
				List<ColumnInfo> columns = MysqlLocalTool.getColumnInfo(methodInfo.getServiceInfo().getProjectInfo(), tableName);
				columns.forEach(column ->{
					if(tmp.contains(column)) {
						tmp.remove(column);
					}
				});
				tmp.addAll(columns);
			}
			methodInfo.getColumns().put(param, tmp);
		}
		List<String> paramNames = new ArrayList<>();
		methodInfo.getColumns().forEach((paramName,columns) -> {
			ParamInfo paramInfo = new ParamInfo();
			paramInfo.setParamName(paramName);
			paramInfo.setColumns(columns);
			paramInfo.setServiceInfo(methodInfo.getServiceInfo());
			paramNames.add(paramName);
			TemplateUtil.getInstance(methodInfo.getServiceInfo().getProjectInfo().getTemplatePath()).build("/maven-project-template/service/BeanTemplate.java", 
					methodInfo.getServiceInfo().getServicesrcPath()+methodInfo.getServiceInfo().getServiceClazzPath()+"/dto/"+NameUtil.getClassName(paramInfo.getParamName())+".java", paramInfo);
		});
		methodInfo.setParanNames(paramNames.toArray(new String[paramNames.size()]));
		TemplateUtil.getInstance(methodInfo.getServiceInfo().getProjectInfo().getTemplatePath()).print("/maven-project-template/service/ServiceMethodTemplate.java", methodInfo);
	}
	
	public static void main(String[] args) {
		MethodMaker.make();
	}
	
}